﻿using System;
using System.Data;
using CodeMinder.Output.MotionChart.Entities;
using NUnit.Framework;
using DataTable = CodeMinder.Output.MotionChart.Entities.DataTable;

namespace CodeMinder.Output.MotionChart.Tests
{
    [TestFixture]
    public class MotionChartTests
    {
        #region Ctor Tests

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Ctor_WithDataTableFirstColumnNotString_ThrowsArgumentException()
        {
            //Arrange:
            var invalidTable = new DataTable();
            var invalidFirstColumn = new DataColumn {DataType = typeof (int)};
            invalidTable.Columns.Add(invalidFirstColumn);
            var secondColumn = new DataColumn { DataType = typeof(DateTime) };
            invalidTable.Columns.Add(secondColumn);
            var thirdColumn = new DataColumn { DataType = typeof(int) };
            invalidTable.Columns.Add(thirdColumn);

            //Act + Assert:
            new Entities.MotionChart(new Options(), invalidTable, "chart_div");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Ctor_WithDataTableSecondColumnNotDateTime_ThrowsArgumentException()
        {
            //Arrange:
            var invalidTable = new DataTable();
            var firstColumn = new DataColumn {DataType = typeof (string)};
            invalidTable.Columns.Add(firstColumn);
            var invalidSecondColumn = new DataColumn {DataType = typeof (int)};
            invalidTable.Columns.Add(invalidSecondColumn);
            var thirdColumn = new DataColumn { DataType = typeof(int) };
            invalidTable.Columns.Add(thirdColumn);

            //Act + Assert:
            new Entities.MotionChart(new Options(), invalidTable, "chart_div");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Ctor_WithDataTableWithOnlyTwoColumns_ThrowsArgumentException()
        {
            //Arrange:
            var invalidTable = new DataTable();
            var firstColumn = new DataColumn { DataType = typeof(string) };
            invalidTable.Columns.Add(firstColumn);
            var invalidSecondColumn = new DataColumn { DataType = typeof(DateTime) };
            invalidTable.Columns.Add(invalidSecondColumn);

            //Act + Assert:
            new Entities.MotionChart(new Options(), invalidTable, "chart_div");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Ctor_WithNullOptions_ThrowsArgumentException()
        {
            //Arrange:
            var table = new DataTable();
            table.Columns.Add(new DataColumn {DataType = typeof (string)});
            table.Columns.Add(new DataColumn {DataType = typeof (DateTime)});
            table.Columns.Add(new DataColumn {DataType = typeof (int)});

            //Act + Assert:
            new Entities.MotionChart(null, table, "chart_div");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Ctor_WithNullContainerId_ThrowsArgumentException()
        {
            //Arrange:
            var table = new DataTable();
            table.Columns.Add(new DataColumn { DataType = typeof(string) });
            table.Columns.Add(new DataColumn { DataType = typeof(DateTime) });
            table.Columns.Add(new DataColumn { DataType = typeof(int) });

            //Act + Assert:
            new Entities.MotionChart(new Options(), table, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Ctor_WithEmptyContainerId_ThrowsArgumentException()
        {
            //Arrange:
            var table = new DataTable();
            table.Columns.Add(new DataColumn { DataType = typeof(string) });
            table.Columns.Add(new DataColumn { DataType = typeof(DateTime) });
            table.Columns.Add(new DataColumn { DataType = typeof(int) });

            //Act + Assert:
            new Entities.MotionChart(new Options(), table, string.Empty);
        }

        #endregion

        #region ToScript Tests

        [Test]
        [Ignore]
        public void ToScript_WithValidDataTableAndOptions_ReturnsMotionChartScript()
        {
            //Arrange:
            var table = new DataTable();
            table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Fruit"});
            table.Columns.Add(new DataColumn { DataType = typeof(DateTime), ColumnName = "Date" });
            table.Columns.Add(new DataColumn { DataType = typeof(int), ColumnName = "Amount" });

            table.Rows.Add(table.NewRow().ItemArray = new object[] { "Apples", new DateTime(1999, 1, 1), 1000 });
            table.Rows.Add(table.NewRow().ItemArray = new object[] { "Oranges", new DateTime(1999, 1, 1), 300 });
            table.Rows.Add(table.NewRow().ItemArray = new object[] { "Bananas", new DateTime(1999, 1, 1), 559 });
            table.Rows.Add(table.NewRow().ItemArray = new object[] { "Apples", new DateTime(1999, 2, 2), 2000 });
            table.Rows.Add(table.NewRow().ItemArray = new object[] { "Oranges", new DateTime(1999, 2, 2), 600 });
            table.Rows.Add(table.NewRow().ItemArray = new object[] { "Bananas", new DateTime(1999, 2, 2), 3000 });

            var options = new Options
                              {
                                  Width = 800,
                                  Height = 400,
                              };
            //Act:
            var script = new Entities.MotionChart(options, table, "chart_div").ToScript();

            //Assert:
            //Assert.Fail(script);
            Console.WriteLine(script);
        }

        #endregion
    }
}
